package com.ybg.base.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;

/**
 * 抽象分页类
 * 
 * @author https://gitee.com/YYDeament/88ybg
 *
 * @date
 * @version v1.0
 */
public abstract class AbstractPage implements Serializable {

	private static final long serialVersionUID = 1L;
	/** 默认每一页的分页大小，如果需要自己定义 使用page.setpageSize(分页大小)即可 **/
	public static Integer DEFAULT_PAGE_SIZE = 10;
	/** 对象获取的开始位置，应该总是从1开始 */
	private Integer startIndex;
	/** 每页获取的页大小，默认是15 **/
	private Integer pageSize = DEFAULT_PAGE_SIZE;
	/** 总记录数 记录一共有多少数据 **/
	private Integer totals;
	/** 应该总是从1开始 */
	private Integer curPage = 1;
	/** 查询的结果 查询后的每一页的分页结果 */
	private List<?> result;
	/** 分页代码 */
	private String paginate;
	/** 总页数 ，总页数等于 总数 除以分页大小 **/
	private Integer totalpages;
	/** layerUi 前段辅助 */
	private int code = 200;
	/** layerUi 错误提示 **/
	private String msg;

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	/**
	 * 自动生成分页语句
	 * 
	 * @param sql
	 *            查询的拼接语句
	 * @return String
	 */
	public abstract String getPagesql(StringBuilder sql);

	/** 自动统计总页数 
	 * @param sql
	 *            查询的拼接语句
	 * @return String
	 */
	/**
	
	* <p>Title: getCountsql</p>
	
	* <p>Description: </p>
	
	* @param sql
	* @return
	String
	
	*/
	public abstract String getCountsql(StringBuilder sql);

	/** 构造方法，只构造空页. */
	public AbstractPage() {
		this(0, 0, DEFAULT_PAGE_SIZE, new ArrayList<Object>());
	}

	/**
	 * 默认构造方法.
	 * 
	 * @param startIndex
	 *            本页数据在数据库中的起始位置
	 * @param totals
	 *            数据库中总记录条数
	 * @param pageSize
	 *            本页容量
	 * @param result
	 *            本页包含的数据 by zhangdan
	 */
	public AbstractPage(Integer startIndex, Integer totals, Integer pageSize, List<?> result) {
		this.pageSize = pageSize;
		this.startIndex = startIndex;
		this.totals = totals;
		this.result = result;
	}

	/**
	 * 获取任一页第一条数据在数据集的位置.
	 * 
	 * @param pageNo
	 *            从1开始的页号
	 * @param pageSize
	 *            每页记录条数
	 * @return 该页第一条数据
	 */
	public static Integer getStartOfPage(Integer pageNo, Integer pageSize) {
		return (pageNo - 1) * pageSize;
	}

	public Integer getTotalpages() {
		return totalpages;
	}

	public void setTotalpages(Integer totalpages) {
		this.totalpages = totalpages;
	}

	@Override
	public String toString() {
		return "Page [getCurPage()=" + getCurPage() + ", getPageSize()=" + getPageSize() + ", getTotalPages()="
				+ getTotalPages() + ", getResult()=" + getResult() + ", getStartIndex()=" + getStartIndex()
				+ ", getTotals()=" + getTotals() + "]";
	}

	public Integer getCurPage() {
		return curPage;
	}

	public void setCurPage(Integer curPage) {
		if (curPage == null || curPage < 1) {
			this.curPage = 1;
			return;
		}
		this.curPage = curPage;
		Integer start = getPageSize() * (curPage - 1) + 1;
		setStartIndex(start);
	}

	public String getPaginate() {
		return paginate;
	}

	public void setPaginate(String paginate) {
		this.paginate = StringUtils.trimToEmpty(paginate);
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	/** 返回分页数 **/
	public Integer getTotalPages() {
		if (totals / this.pageSize < 1) {

			return 1;
		} else {
			Integer totalPages = this.totals / this.pageSize;
			if (this.totals % this.pageSize != 0) {
				totalPages += 1;
			}
			return totalPages;
		}
	}

	public List<?> getResult() {

		return result;
	}

	public void setResult(List<?> result) {
		setTotalpages(getTotalPages());
		this.result = result;
	}

	public Integer getStartIndex() {
		return startIndex;
	}

	/**
	 * 查询的开始位置总是从1开始.
	 * 
	 * @param startIndex
	 */
	public void setStartIndex(Integer startIndex) {
		this.startIndex = startIndex;
		if (this.startIndex <= 0) {
			this.startIndex = 1;
		}
		setTotalpages(getTotalPages());
	}

	public Integer getTotals() {
		return totals;
	}

	public void setTotals(Integer totals) {
		this.totals = totals;
	}
}
